package com.order.hit.crossover.platform.common.dao;

import com.order.hit.crossover.platform.common.bean.order.Order;
import com.order.hit.crossover.platform.common.bean.order.OrderState;
import com.order.hit.crossover.platform.common.bean.Good;
import com.order.hit.crossover.platform.common.bean.Store;
import com.order.hit.crossover.platform.common.bean.order.OrderType;
import com.order.hit.crossover.platform.common.utils.TimeChange;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/8/4
 * 订单存储在数据库中的类型，
 */
@Getter
@Setter
@ToString
public class OrderDao {
    // order
    private String orderId;
    private String orderType;
    private String orderState;

    // store
    private String storeId;
    private String storeName;
    private String storeLocation;

    // good
    private String goodId;
    private String goodName;
    private String goodType;
    private String spec;
    private double wholeSalePrice;
    private double retailPrice;
    private String validDate;

    /**
     * 预期价格
     */
    private double price;
    private int goodNumber;

    // 开始时间与停止时间
    private String start_time;
    private String end_time;

//    //展位费用， 求购为0，求售另算。
//    private double exhibition_fee;


    public static OrderDao getOrderDao(Order order){
        // TODO
        OrderDao orderDao = new OrderDao();

        orderDao.setOrderId(order.getOrderId());
        orderDao.setOrderType(order.getOrderType().toString());
        orderDao.setOrderState(order.getOrderState().toString());

        orderDao.setStoreId(order.getStore().getStoreId());
        orderDao.setStoreName(order.getStore().getStoreName());
        orderDao.setStoreLocation(order.getStore().getLocation());

        orderDao.setGoodId(order.getGood().getGoodId());
        orderDao.setGoodName(order.getGood().getGoodName());
        orderDao.setGoodType(order.getGood().getGoodType());
        orderDao.setSpec(order.getGood().getSpec());
        orderDao.setWholeSalePrice(order.getGood().getWholeSalePrice());
        orderDao.setRetailPrice(order.getGood().getRetailPrice());
        orderDao.setValidDate(TimeChange.getDateString(order.getGood().getValidDate()));


        orderDao.setPrice(order.getPrice());
        orderDao.setGoodNumber(order.getGoodNumber());

        orderDao.setStart_time(TimeChange.getDateString(order.getStart_time()));
        orderDao.setEnd_time(TimeChange.getDateString(order.getEnd_time()));

        return orderDao;
    }


    public static Order getOrder(OrderDao orderDao){
        // TODO
        Order order = new Order();
        order.setOrderId(orderDao.getOrderId());
        order.setOrderState(OrderState.valueOf(orderDao.getOrderState()));
        order.setOrderType(OrderType.valueOf(orderDao.getOrderType()));


        Store store = new Store();
        store.setStoreId(orderDao.getStoreId());
        store.setStoreName(orderDao.getStoreName());
        store.setLocation(orderDao.getStoreLocation());
        store.setPassword(null);
        order.setStore(store);

        Good good = new Good(
                orderDao.getGoodId(),
                orderDao.getGoodName(),
                orderDao.getGoodType(),
                orderDao.getSpec(),
                orderDao.getWholeSalePrice(),
                orderDao.getRetailPrice(),
                TimeChange.getDate(orderDao.getValidDate()));
        order.setGood(good);

        order.setPrice(orderDao.getPrice());
        order.setGoodNumber(orderDao.getGoodNumber());

        order.setStart_time(TimeChange.getDate(orderDao.getStart_time()));
        order.setEnd_time(TimeChange.getDate(orderDao.getEnd_time()));

        return order;
    }


    public static List<Order> getListOrder(List<OrderDao> list){
        List<Order> lists = new ArrayList<>();
        for (OrderDao orderDao: list){
            lists.add(getOrder(orderDao));
        }
        return lists;
    }

    public static List<OrderDao> getListDao(List<Order> list){
        List<OrderDao> lists = new ArrayList<>();
        for (Order o: list){
            lists.add(getOrderDao(o));
        }
        return lists;
    }
}
